package com.qf.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.qf.dao.PermissionMapper;
import com.qf.pojo.BaseResult;
import com.qf.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by 54110 on 2021/4/30.
 */
@Component
public class MyInterceptor implements HandlerInterceptor {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    JwtUtils jwtUtils;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从前端请求的cookie中 获取 uuid 通过uuid从redis中获取 对象，如能 获取到则以登录，不能则返回登录失效
        Cookie[] cookies =  request.getCookies();
        String token="";
        if (cookies!=null){
            for (Cookie cookie: cookies
                 ) {
                if (cookie.getName().equals("token")){
                    token=cookie.getValue();
                }
            }
        }
        if (token.isEmpty()){
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json");
            response.getWriter().write(JSONObject.toJSONString(new BaseResult(1,"用户未登录!")));
            return  false;
        }
        //从redis中获取
        //Object o = redisTemplate.opsForValue().get(rediskey);
        //使用jwt进行解析
        Map map = jwtUtils.varferToken(token);
        if (map==null){
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json");
            response.getWriter().write(JSONObject.toJSONString(new BaseResult(1,"用户登录失效，从新登录！")));
            return  false;
        }
        //request.setAttribute("userid",Integer.valueOf(map.get("id").toString()));
        //String string = JSONObject.toJSONString(o);
        //TbUser tbUser = JSONObject.parseObject((String)o, TbUser.class);
       // String uername = map.get("username").toString();
        //验证权限
//        List<Permission> byUserName = permissionMapper.findByUserName(uername);
//        List  perList= new ArrayList<>();
//        for (Permission list:byUserName
//             ) {
//            perList.add(list.getPermissionName());
//        }
        //获取到方法名称
//        String method1 = request.getMethod();
//        request.get
//        String requestURI = request.getRequestURI();
//        String[] split = requestURI.split("/");
//        if(!perList.contains(split[2])){
//            response.setCharacterEncoding("utf-8");
//            response.setContentType("application/json");
//            response.getWriter().write(JSONObject.toJSONString(new BaseResult(1,"用户没权限，请氪金！")));
//            return  false;
//        }
//        //向本次 请求 中设置 当前用户的 id
//        request.setAttribute("id",tbUser.getId());
        //request.setp
        return true;
    }
}
